<?php

namespace Biz\tools;

use Biz\model\wiki\DingDanXiaGoods as mDingDanXiaGoods;
use Biz\pockets\serviceTrait\_Async;
use Doraemon\tools\Curl;
use Doraemon\tools\Log;
use GuzzleHttp\RequestOptions;

class DingDanXia
{
    use _Async;

    const API_TAOBAO_URL = 'http://api.ds.dingdanxia.com/shop/good_info';//淘宝商品信息(淘宝全网商品详情v2.0版) 已下线
    const API_TAOBAO_DESC_URL = 'http://api.ds.dingdanxia.com/shop/good_images';//淘宝图文详情
    const API_TKL_QUERY_URL = 'http://api.tbk.dingdanxia.com/tkl/query';//淘口令解析api接口（加强版,支持所有口令）
    const API_TBK_PRIVILEGE = 'http://api.tbk.dingdanxia.com/tbk/id_privilege';//淘宝客高佣转链api（商品id版）
    const API_TAOBAO_SIMPLE_URL = 'http://api.ds.dingdanxia.com/shop/item_info';//淘宝全网商品详情(简版) 已下线
    const API_TBK_ITEM_DETAIL_URL = 'http://api.tbk.dingdanxia.com/tbk/item_detailinfo';//淘宝客商品详情查询(详细版)

    const API_JD_ITEM_DETAIL_URL = 'http://api.tbk.dingdanxia.com/jd/item_detail';//京东商品详情查询接口
    const API_JD_QUREY_GOODS = 'http://api.tbk.dingdanxia.com/jd/query_goods';//京东商品URL链接/关键词/类目/相似推荐查询/商品ID查询优惠券列表接口（京东超级搜索接口）

    const API_KEY = 'oRRz8pCn5K9wHIPY8O1cFHpIucSGurur';
    const API_GOODS_INFO_SUCCESS = 200;

    public static function getInfo($type, $itemId, $goodsDesc = true)
    {
        $info = [];
        switch ($type) {
            case RealUrl::URL_TYPE_TAOBAO:
            case RealUrl::URL_TYPE_TMALL:
                $result = self::getTaoBaoGoodsInfo($itemId);
                if ($result) {
                    $result['desc_images'] = $goodsDesc ? self::getTaobaoGoodsImgs($itemId) : [];
                    $info = self::formatTaoBaoData($type, $result);
                } else {
                    $simpleInfo = self::getTaoBaoSimpleGoodsInfo($itemId);
                    $simpleInfo['id'] = $itemId;
                    $simpleInfo['desc_images'] = $goodsDesc ? self::getTaobaoGoodsImgs($itemId) : [];
                    $info = self::formatSimpleTaoBaoData($type, $simpleInfo);
                }
                break;
            case RealUrl::URL_TYPE_JD:
                $goodsInfo = self::getJDGoodsInfo($itemId);
                $itemDetail = self::getJDItemDetail($itemId);
                $data = [];
                if ($goodsInfo && $goodsInfo['data'] && $goodsInfo['data'][0]) {
                    $data['goods_info'] = $goodsInfo['data'][0];
                }
                if ($itemDetail && $itemDetail['data'] && $itemDetail['data'][0]) {
                    $data['item_detail'] = $itemDetail['data'][0];
                }
                $info = $data ? self::formatJDData($data) : [];
                break;
        }
        if ($info) {
            $data = [
                'type' => $info['channel_type'],
                'item_id' => $info['channel_spu_id'],
                'name' => $info['title'],
                'shop_id' => $info['channel_shop_id'],
                'shop_name' => $info['channel_shop_name'],
                'min_price' => $info['min_price'],
                'max_price' => $info['max_price'],
                'category_name' => $info['channel_category_name'],
                'category_id' => $info['channel_category_id'],
                'brand_name' => $info['channel_brand'],
                'cover_img' => $info['cover_img'],
                'imgs' => $info['imgs'],
                'desc_imgs' => $info['desc_imgs'],
                'group_props' => $info['group_props']
            ];
            self::addData($data);
        }
        return $info;
    }

    /**
     * 获取淘宝客商品详情
     * @param $type
     * @param $itemId
     * @return array
     */
    public static function getTbkItemInfo($type, $itemId)
    {
        $goodsInfo = [];
        Curl::$defaultConfig[RequestOptions::TIMEOUT] = 5;
        $result = Curl::Request('POST', self::API_TBK_ITEM_DETAIL_URL, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'id' => $itemId,
                'is_desimg' => 1,
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                $goodsInfo = $result['json']['data'];
            } else {
                Log::debug('淘宝客商品详情查询失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('淘宝客商品详情查询,curl请求失败。#code:' . $result['code']);
        }
        $info = $goodsInfo ? self::formatTbkData($goodsInfo, $type) : [];
        return $info;
    }

    private static function addData($data)
    {
        if ($data) {
            self::_async($data);
        }
    }

    public static function _async_addData($data)
    {
        mDingDanXiaGoods::addData($data);
    }

    public static function getTaoBaoGoodsInfo($itemId)
    {
        $goodsInfo = [];
        return $goodsInfo;//官方已不提供接口服务
        Curl::$defaultConfig[RequestOptions::TIMEOUT] = 5;
        $result = Curl::Request('POST', self::API_TAOBAO_URL, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'id' => $itemId,
                'info' => 'all',
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                $goodsInfo = $result['json']['data']['data'];
            } else {
                Log::debug('获取淘宝商品失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('获取淘宝商品,curl请求失败。#code:' . $result['code']);
        }
        return $goodsInfo;
    }

    public static function getTaobaoGoodsImgs($itemId)
    {
        Curl::$defaultConfig[RequestOptions::TIMEOUT] = 5;
        $result = Curl::Request('POST', self::API_TAOBAO_DESC_URL, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'id' => $itemId
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                return $result['json']['data'];
            } else {
                Log::debug('获取淘宝商品图片详情失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('获取淘宝商品图片详情,curl请求失败。#code:' . $result['code']);
        }
    }

    public static function getTaoBaoSimpleGoodsInfo($itemId)
    {
        $goodsInfo = [];
        return $goodsInfo;//官方已不提供接口服务
        Curl::$defaultConfig[RequestOptions::TIMEOUT] = 5;
        $result = Curl::Request('POST', self::API_TAOBAO_SIMPLE_URL, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'id' => $itemId,
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                $goodsInfo = $result['json']['data'];
            } else {
                Log::debug('获取淘宝商品(简版)失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('获取淘宝商品（简版）,curl请求失败。#code:' . $result['code']);
        }
        return $goodsInfo;
    }

    public static function getJDGoodsInfo($itemId)
    {
        Curl::$defaultConfig[RequestOptions::TIMEOUT] = 5;
        $result = Curl::Request('POST', self::API_JD_QUREY_GOODS, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'skuIds' => $itemId,
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                return $result['json'];
            } else {
                Log::debug('获取京东商品失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('获取京东商品,curl请求失败。#code:' . $result['code']);
        }
        return [];
    }

    public static function getJDItemDetail($itemId)
    {
        Curl::$defaultConfig[RequestOptions::TIMEOUT] = 5;
        $result = Curl::Request('POST', self::API_JD_ITEM_DETAIL_URL, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'skuIds' => $itemId,
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                return $result['json'];
            } else {
                Log::debug('获取京东商品详情失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('获取京东商品详情失败,curl请求失败。#code:' . $result['code']);
        }
        return [];
    }

    private static function formatTbkData($data, $type)
    {
        $descImages = $data['desc_images'] ?? [];
        $smallImgs = $data['small_images']['string'] ?? [];

        $price = $data['zk_final_price'] ?? 0;

        $priceArr = [];
        if (isset($data['sku_list']['tbk_item_detail_sku'])) {
            if (isset($data['sku_list']['tbk_item_detail_sku']['sku_final_price'])) {
                $priceArr[] = $data['sku_list']['tbk_item_detail_sku']['sku_final_price'];
            } else if (isset($data['sku_list']['tbk_item_detail_sku'][0]['sku_final_price'])) {
                foreach ($data['sku_list']['tbk_item_detail_sku'] as $row) {
                    $priceArr[] = $row['sku_final_price'];
                }
            }
        }
        $maxPrice = $priceArr ? max($priceArr) : $price;
        $minPrice = $priceArr ? min($priceArr) : $price;
        $coverImg = $data['pict_url'] ?? '';
        $imgs = [];
        foreach ($smallImgs as $imgRow) {
            $parseUrlArr = parse_url($imgRow);
            $imgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
            if (!$coverImg) {
                $coverImg = $imgUrl;
            }
            $imgs[] = $imgUrl;
        }
        if (empty($imgs) && $coverImg) {
            $imgs[] = $coverImg;
        }

        $content = '';
        foreach ($descImages as $descImgsRow) {
            $parseUrlArr = parse_url($descImgsRow);
            $detailImgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
            $content .= '<img src="' . $detailImgUrl . '">';
        }
        $info = [
            'title' => $data['title'],
            'channel_type' => $type,
            'channel_spu_id' => $data['item_id'],
            'channel_seller_id' => $data['seller_id'] ?? 0,
            'channel_shop_id' => 0,//未提供
            'channel_shop_name' => $data['nick'] ?? '',
            'channel_category_name' => $data['category_name'] ?? '',
            'channel_category_id' => 0,//未提供
            'channel_brand' => '',//未提供
            'min_price' => $minPrice,
            'max_price' => $maxPrice,
            'cover_img' => $coverImg,
            'imgs' => $imgs ? json_encode($imgs, JSON_UNESCAPED_UNICODE) : '',
            'content' => $content,
        ];
        return $info;
    }

    private static function formatTaoBaoData($type, $data)
    {
        $seller = $data['seller'];
        $item = $data['item'];
        $descImages = $data['desc_images'];
        $mockData = $data['mockData'] ? json_decode($data['mockData'], true) : [];

        $apiStackData = isset($data['apiStack'][0]['value']) ? $data['apiStack'][0]['value'] : [];
        $apiStackData = $apiStackData ? json_decode($apiStackData, true) : [];

        $groupProps = isset($data['props']['groupProps'][0]) ? $data['props']['groupProps'][0] : [];
        $priceArr = [];
        if ($apiStackData && isset($apiStackData['skuCore']['sku2info'])) {
            $priceData = isset($apiStackData['skuCore']['sku2info']) ? array_values($apiStackData['skuCore']['sku2info']) : [];
            foreach ($priceData as $row) {
                if (is_numeric($row['price']['priceText'])) {
                    $priceArr[] = $row['price']['priceText'];
                }
            }
        }
        if (!$priceArr) {
            if ($mockData) {
                $priceArr[] = $mockData['price']['price']['priceText'];
                $priceData = isset($mockData['skuCore']['sku2info']) ? array_values($mockData['skuCore']['sku2info']) : [];
                foreach ($priceData as $row) {
                    if (is_numeric($row['price']['priceText'])) {
                        $priceArr[] = $row['price']['priceText'];
                    }
                }
            }
        }
        $maxPrice = $priceArr ? max($priceArr) : 0;
        $minPrice = $priceArr ? min($priceArr) : 0;
        $propsBaseArr = isset($groupProps['基本信息']) ? $groupProps['基本信息'] : [];
        $brandName = '';
        $material = [];//材质
        $style = [];//风格
        foreach ($propsBaseArr as $row) {
            if (isset($row['品牌'])) {
                $brandName = $row['品牌'];
            } else if (isset($row['风格'])) {
                $style[] = $row['风格'];
            } else if (isset($row['材质'])) {
                $material[] = $row['材质'];
            } else {
                foreach ($row as $key => $val) {
                    if (strpos($key, '材质') !== false && $val && $val != '无') {
                        $material[] = $val;
                    }
                }
            }
        }
        $tags = [];
        if (!empty($style)) {
            $tags['style'] = array_values(array_unique($style));
        }
        if (!empty($material)) {
            $tags['material'] = array_values(array_unique($material));
        }
        $coverImg = '';
        $imgs = [];
        foreach ($item['images'] as $imgRow) {
            $parseUrlArr = parse_url($imgRow);
            $imgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
            if (!$coverImg) {
                $coverImg = $imgUrl;
            }
            $imgs[] = $imgUrl;
        }

        $content = '';
        foreach ($descImages as $descImgsRow) {
            $parseUrlArr = parse_url($descImgsRow);
            $detailImgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
            $content .= '<img src="' . $detailImgUrl . '">';
        }
        $info = [
            'title' => $item['title'],
            'channel_type' => $type,
            'channel_spu_id' => $item['itemId'],
            'channel_shop_id' => $seller ? $seller['shopId'] : 0,
            'channel_shop_name' => $seller ? $seller['shopName'] : '',
            'channel_category_name' => '',
            'channel_category_id' => $item['categoryId'],
            'channel_brand' => $brandName,
            'min_price' => $minPrice,
            'max_price' => $maxPrice,
            'cover_img' => $coverImg,
            'imgs' => $imgs ? json_encode($imgs, JSON_UNESCAPED_UNICODE) : '',
            'desc_imgs' => $descImages ? json_encode($descImages, JSON_UNESCAPED_UNICODE) : '',
            'content' => $content,
            //'channel_spu_status' => 1,
            'group_props' => !empty($groupProps) ? json_encode($groupProps, JSON_UNESCAPED_UNICODE) : '',
            'tags' => !empty($tags) ? json_encode($tags, JSON_UNESCAPED_UNICODE) : '',
        ];
        return $info;
    }

    private static function formatSimpleTaoBaoData($type, $data)
    {
        $descImages = $data['desc_images'];

        $coverImg = $data['pict_url'] ?? '';
        $coverImg = empty($coverImg) ? '' : $coverImg;
        if ($coverImg) {
            $parseUrlArr = parse_url($coverImg);
            $coverImg = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
        }

        $imgs = [];
        if (!empty($data['images'])) {
            foreach ($data['images'] as $imgRow) {
                $parseUrlArr = parse_url($imgRow);
                $imgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
                if (!$coverImg) {
                    $coverImg = $imgUrl;
                }
                $imgs[] = $imgUrl;
            }
        }


        $content = '';
        if (!empty($descImages)) {
            foreach ($descImages as $descImgsRow) {
                $parseUrlArr = parse_url($descImgsRow);
                $detailImgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
                $content .= '<img src="' . $detailImgUrl . '">';
            }
        }
        $info = [
            'title' => $data['title'] ?? '',
            'channel_type' => $type,
            'channel_spu_id' => $data['id'] ?? 0,
            'channel_shop_id' => $data['shopID'] ?? 0,
            'channel_shop_name' => $data['shopName'] ?? '',
            'channel_category_name' => '',
            'channel_category_id' => 0,
            'channel_brand' => '',
            'min_price' => $data['price'] ?? 0,
            'max_price' => $data['price'] ?? 0,
            'cover_img' => $coverImg,
            'imgs' => $imgs ? json_encode($imgs, JSON_UNESCAPED_UNICODE) : '',
            'desc_imgs' => $descImages ? json_encode($descImages, JSON_UNESCAPED_UNICODE) : '',
            'content' => $content,
            'group_props' => '',
            //'channel_spu_status' => (isset($data['status']) && $data['status'] <= 0) ? 0 : 1,
        ];
        return $info;
    }

    private static function formatJDData($data)
    {
        $info = [];
        $goodsInfo = isset($data['goods_info']) ? $data['goods_info'] : [];
        $itemDetail = isset($data['item_detail']) ? $data['item_detail'] : [];
        if ($goodsInfo) {
            $itemId = $goodsInfo['skuId'];
            $name = $goodsInfo['skuName'];
            $price = isset($goodsInfo['priceInfo']['price']) ? $goodsInfo['priceInfo']['price'] : 0;
            $shopInfo = isset($goodsInfo['shopInfo']) ? $goodsInfo['shopInfo'] : [];
            $shopId = $shopInfo ? $shopInfo['shopId'] : 0;
            $shopName = $shopInfo ? $shopInfo['shopName'] : '';
            $categoryName = isset($goodsInfo['categoryInfo']['cid1Name']) ? $goodsInfo['categoryInfo']['cid1Name'] : '';
            $categoryId = isset($goodsInfo['categoryInfo']['cid1']) ? $goodsInfo['categoryInfo']['cid1'] : 0;
            $brandName = $goodsInfo['brandName'];
            $coverImg = '';
            $imgs = [];
            foreach ($goodsInfo['imageInfo']['imageList'] as $row) {
                $parseUrlArr = parse_url($row['url']);
                $imgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
                if (!$coverImg) {
                    $coverImg = $imgUrl;
                }
                $imgs[] = $imgUrl;
            }
            $info = [
                'title' => $name,
                'channel_type' => RealUrl::URL_TYPE_JD,
                'channel_spu_id' => $itemId,
                'channel_shop_id' => $shopId,
                'channel_shop_name' => $shopName,
                'channel_category_name' => $categoryName,
                'channel_category_id' => $categoryId,
                'channel_brand' => $brandName,
                'min_price' => $price,
                'max_price' => $price,
                'cover_img' => $coverImg,
                'imgs' => $imgs ? json_encode($imgs, JSON_UNESCAPED_UNICODE) : '',
                'desc_imgs' => '',
                'content' => '',
                'group_props' => '',
                //'channel_spu_status' => 1
            ];
        }
        if ($itemDetail && $itemDetail['detailImages'] && isset($itemDetail['skuId'])) {
            $itemId = $itemDetail['skuId'];
            $name = $itemDetail['skuName'];

            //不存在的信息
            $shopId = 0;
            $shopName = '';
            $price = 0;
            $brandName = '';

            $categoryName = isset($itemDetail['categoryInfo']['cid1Name']) ? $itemDetail['categoryInfo']['cid1Name'] : '';
            $categoryId = isset($itemDetail['categoryInfo']['cid1']) ? $itemDetail['categoryInfo']['cid1'] : 0;
            $skuStatus = isset($itemDetail['skuStatus']) ? $itemDetail['skuStatus'] : 1;

            $coverImg = '';
            $imgs = [];
            foreach ($itemDetail['imageInfo']['imageList'] as $row) {
                $parseUrlArr = parse_url($row['url']);
                $imgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
                if (!$coverImg) {
                    $coverImg = $imgUrl;
                }
                $imgs[] = $imgUrl;
            }
            $descImgs = (isset($itemDetail['detailImages']) && !empty($itemDetail['detailImages'])) ? $itemDetail['detailImages'] : '';
            $descImgs = !empty($descImgs) ? explode(',', $descImgs) : '';
            $content = '';
            foreach ($descImgs as $descImgsRow) {
                $parseUrlArr = parse_url($descImgsRow);
                $detailImgUrl = 'https://' . $parseUrlArr['host'] . $parseUrlArr['path'];
                $content .= '<img src="' . $detailImgUrl . '">';
            }
            $info2 = [
                'title' => $name,
                'channel_type' => RealUrl::URL_TYPE_JD,
                'channel_spu_id' => $itemId,
                'channel_shop_id' => $shopId,
                'channel_shop_name' => $shopName,
                'channel_category_name' => $categoryName,
                'channel_category_id' => $categoryId,
                'channel_brand' => $brandName,
                'min_price' => $price,
                'max_price' => $price,
                'cover_img' => $coverImg,
                'imgs' => $imgs ? json_encode($imgs, JSON_UNESCAPED_UNICODE) : '',
                'desc_imgs' => $descImgs ? json_encode($descImgs, JSON_UNESCAPED_UNICODE) : '',
                'content' => $content,
                'group_props' => '',
                //'channel_spu_status' => ($skuStatus <= 0) ? 0 : $skuStatus
            ];

            if (empty($info)) {
                $info = $info2;
            } else {
                foreach ($info as $k => $v) {
                    if (empty($v)) {
                        $info[$k] = $info2[$k];
                    }
                }
            }
        }
        return $info;
    }

    public static function tklDecrypt($content)
    {
        $result = Curl::Request('POST', self::API_TKL_QUERY_URL, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'tkl' => $content,
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                return $result['json']['data'];
            } else {
                Log::debug('获取淘口令失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
            }
        } else {
            Log::debug('获取淘口令失败,curl请求失败。#code:' . $result['code']);
        }
        return false;
    }

    public static function getTklUrl($content)
    {
        $data = self::tklDecrypt($content);
        if ($data && isset($data['num_iid'])) {
            $goodsId = $data['num_iid'];
            if ($goodsId) {
                return RealUrl::PROTOCOL . RealUrl::ITEM_TAOBAO_HOST . '/item.html?id=' . $goodsId;
            }
        }
        return false;
    }

    public static function getPrivilegeItemUrl($itemId)
    {
        $itemUrl = '';
        $result = self::getIdPrivilege($itemId);
        if ($result && isset($result['item_url'])) {
            $itemUrl = $result['item_url'];
        }
        return $itemUrl;
    }

    public static function getIdPrivilege($itemId)
    {
        $result = Curl::Request('POST', self::API_TBK_PRIVILEGE, [
            'form_params' => [
                'apikey' => self::API_KEY,
                'id' => $itemId,
            ]
        ]);
        if ($result['code'] == Curl::CURL_REQUEST_SUCCESS) {
            if ($result['json']['code'] == self::API_GOODS_INFO_SUCCESS) {
                return $result['json']['data'];
            } else {
                Log::debug('获取淘口令失败。#code:' . $result['json']['code'] . '#msg:' . json_encode($result['json'], JSON_UNESCAPED_UNICODE));
                $msg = isset($result['json']['msg']) ? $result['json']['msg'] : '';
                if (in_array($result['json']['code'], [4013, 4014]) || ($msg && strpos($msg, '授权') !== false)) {
                    Log::debug('getIdPrivilege error#' . $itemId, $result);
                    try {
                        Mail::send('tangyiwan@haohaozhu.com', 'getIdPrivilege error#' . $itemId, json_encode($result, JSON_UNESCAPED_UNICODE));
                    } catch (\Exception $e) {
                        Log::error($e->getMessage());
                    }
                }
            }
        } else {
            Log::debug('获取淘口令失败,curl请求失败。#code:' . $result['code']);
        }
        return false;
    }
}